#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int hash[100001] = { 0 };
        int len = 0, n = fruits.size(), sum = 0;
        for (int left = 0, right = 0; right < n; right++)
        {

            if (hash[fruits[right]] == 0)
            {
                sum++;
            }
            hash[fruits[right]]++;
            while (sum > 2)
            {
                hash[fruits[left]]--;
                if (hash[fruits[left]] == 0) sum--;
                left++;
            }
            len = max(len, right - left + 1);
        }
        return len;
    }
};